#include <cbm.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include "global.h"
#include "globalInput.h"
#include "DriveRoutines.h"
#include "Screen.h"

#ifdef __CBM__
static ds_t drives[] =
{
	{ 8, false, "", { {0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0}} },	// 0
	{ 9, false, "", { {0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0}}  },	// 1
	{ 10, false, "", { {0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0}}  },	// 2
	{ 11, false, "", { {0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0}}  },	// 3
	{ 12, false, "", { {0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0}}  },	// 4
	{ 13, false, "", { {0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0}}  },	// 5
	{ 14, false, "", { {0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0}}  },	// 6
	{ 15, false, "", { {0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0}}  },	// 7
	{ 16, false, "", { {0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0}}  },	// 8
	{ 0, false, "", { {0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0},{0}}  } // Special case for non-standard drive numbers
};
#endif

// Use this function, instead of cbm_open(), to open files.  It checks the DOS
// status message.  It returns a value greater than zero if the device cannot
// be found.  It returns a negative value if the DOS reports an error;
// then, the buffer array holds the error string.
signed char __fastcall__ cbmOpen(unsigned char lfn, unsigned char device,
	unsigned char sec_addr, const char* name,
	unsigned char errf)
{
	static signed char r;

	// Open the drive's command/status channel.
	if (errf != 0)
	{
		cbm_open(errf, device, 15, "");
	}

	if ((r = cbm_open(lfn, device, sec_addr, name)) == 0 && errf != 0)
	{
		// Check the DOS status.
		r = cbm_read(errf, buffer, (sizeof buffer) - 1);
		buffer[r < 0 ? 0 : r] = '\0';
		r = (buffer[0] != '0') ? -1 : 0;
	}

	return r;
}

signed char __fastcall__ sendCommand(
	const unsigned char drive,
	const char *command,
	char *results)
{
#ifdef __CBM__
	static signed char result;
	signed char r = 0;

	if((r = cbm_open(15, drive, 15, command)) == 0)
	{
		result = cbm_read(15, buffer, (sizeof buffer) - 1);
		buffer[result < 0 ? 0 : result] = '\0';

		cbm_close(15);
		
		strcpy(results, buffer);

	}
	else
	{
		waitForEnterEscf("r: %u  _oserror: %u", r, _oserror);
	}

	// Show error messages.
	//if (buffer[0] != '0')
	//{
	//	waitForEnterEscf(buffer);
	//	return -1;		// A negative return-value means that command failed.
	//}
#endif
	return 0;
}

static int __fastcall__ getDriveStatus(
	ds_t *drive)
{
	int size;

	if(cbm_open(15, drive->drive, 15, "") != 0)
	{
		waitForEnterEscf("_oserror: %u", _oserror);
		cbm_close(15);

		return -1;
	}

	size = cbm_read(15, drive->message, (sizeof drive->message) - 1);
	if(size > 0)
	{
		cbm_write(15, "ui", 2);
		size = cbm_read(15, drive->message, (sizeof drive->message) - 1);
	}
	drive->message[size < 0 ? 0 : size] = '\0';

	cbm_close(15);
	return size;
}

void enumerateDrives(const unsigned char initialDrive)
{
	unsigned char i;
	unsigned char unknownDriveMessage[31];
	unsigned char title[10];
	static const char* const dialogMessage[] =
	{
		{ "Enter name for this drive:" }
	};

	if(initialDrive < 8)
	{
		sprintf(title, "Drive %d", initialDrive);
		drawInputDialog(1, 30, dialogMessage, title, unknownDriveMessage);
		strcpy(drives[9].message, unknownDriveMessage);
		drives[9].drive = initialDrive;
		drives[9].isSelected = true;
	}

	if(initialDrive > 16)
	{
		drives[9].drive = initialDrive;
		writeStatusBarf("Interrogating drive %d.", initialDrive);
		
		if(checkDrive(initialDrive)==0)
		{
			getDriveStatus(&drives[9]);
			drives[9].isSelected = true;
		}
	}

	for(i = 0; i < 9; i++)
	{
		writeStatusBarf("Interrogating drive %d.", drives[i].drive);
		if(checkDrive(drives[i].drive)==0)
		{
			getDriveStatus(&drives[i]);
			if(drives[i].drive == initialDrive)
				drives[i].isSelected = true;
		}
	}
}